package com.jmyu.meetappointment.ui;

import java.util.List;

import cn.bmob.v3.Bmob;
import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.BmobUser;
import cn.bmob.v3.listener.FindListener;
import cn.bmob.v3.listener.SaveListener;

import com.jmyu.meetappointment.R;
import com.jmyu.meetappointment.bean.MyUser;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Toast;

public class LoginActivity extends Activity {
	private final static String TAG = "LoginActivityTag";
	private ImageButton mLoginBtn;
	private EditText mNameEdit;
	private EditText mPassEdit;
	private String name;
	private String password;
	private ProgressDialog dialog;
	public int what;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Bmob.initialize(this, "dc6fbc9930ef469fb770c16bb7dac92a");
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.login);
		initView();
		setListener();		
	}

	private void setListener() {
		mLoginBtn.setOnClickListener(new View.OnClickListener() {		
			@Override
			public void onClick(View v) {	    
				name=mNameEdit.getText().toString();
				password=mPassEdit.getText().toString();
				Log.d(TAG, name+":"+password);
				if(name.equals("")){
					mNameEdit.setError("用户名不能为空");
					return;
				}
				if(password.equals("")){
					mPassEdit.setError("密码不能为空");
					return;
				}
				if(!LoginActivity.this.dialog.isShowing()){
					LoginActivity.this.dialog.show();
				}				
				Log.d(TAG, "打开Dialog"+dialog.isShowing()+"");
				doLogin(name, password);
				}
		});		
	}
    
	private void initView() {
		mLoginBtn = (ImageButton) findViewById(R.id.login_btn);
		mNameEdit=(EditText)findViewById(R.id.unameEdit);
		mPassEdit=(EditText)findViewById(R.id.upassEdit);
        dialog=new ProgressDialog(this);
        dialog.setMessage("welcome to Easy Appointment,please wait..");
	    
        
	}
	
	
	private void doLogin(String name, String pw) {
		final String fname = name;
		final String fpw = pw;
		BmobQuery<MyUser> query = new BmobQuery<MyUser>();
		query.addWhereEqualTo("password", pw);
		query.addWhereEqualTo("username",name);
		query.findObjects(LoginActivity.this, new FindListener<MyUser>() {
			
			@Override
			public void onSuccess(List<MyUser> list) {
				if(LoginActivity.this.dialog.isShowing()){
	        		LoginActivity.this.dialog.dismiss();
	        	}
				if(!list.isEmpty()){
					Log.d(TAG, "查询得："+list.get(0).getUsername()+":"+list.get(0).getPassword());
					Intent intent=new Intent(LoginActivity.this,MainActivity.class);
					intent.putExtra("loginname", fname);
					intent.putExtra("loginpw", fpw);
					startActivity(intent);
					finish();
				}else{
					Toast.makeText(LoginActivity.this, "用户名或密码错误,请确认!", 1).show();
				}
				
			}
			
			@Override
			public void onError(int arg0, String arg1) {
				Log.d(TAG, arg1);
			}
		});
		
	}
	
}
